package com.javaweb.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.NcPaybill;
import com.javaweb.admin.entity.NcPaybillDetail;
import com.javaweb.admin.mapper.NcPaybillDetailMapper;
import com.javaweb.admin.mapper.NcPaybillMapper;
import com.javaweb.common.annotation.Log;
import com.javaweb.common.common.BaseController;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.enums.LogType;
import com.javaweb.common.utils.JsonResult;
import com.seegw.shaphar.NCInterface;
import com.sphchina.esb.webservice.Items;
import com.sphchina.esb.webservice.PaybillData;
import com.sphchina.esb.webservice.ISendToReviewLog;
import com.sphchina.esb.webservice.NCDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/esbservice")
public class ESBController extends BaseController {

    @Autowired
    private ISendToReviewLog sendToReviewLog;

    @Autowired
    private NcPaybillMapper ncPaybillMapper;
    @Autowired
    private NcPaybillDetailMapper ncPaybillDetailMapper;

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/sendorder")
    public JsonResult sendorder(@RequestBody String body) {
        //String body = JSON.toJSONString(entity);
        List<NcPaybill> list = ncPaybillMapper.selectList(new QueryWrapper<NcPaybill>().eq("status","0").eq("billclass","fk"));
        NCDto dto = new NCDto();
        List<NcPaybillDetail> detailList;
        PaybillData billdata = new PaybillData();
        List<Items> listItems = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BeanUtils.copyProperties(list.get(i),billdata);
            billdata.setPk_balatype(list.get(i).getPkBalatype());
            billdata.setPk_busitype(list.get(i).getPkBusitype());
            billdata.setPk_currtype(list.get(i).getPkCurrtype());
            billdata.setPk_deptid(list.get(i).getPkDeptid());
            billdata.setPk_group(list.get(i).getPkGroup());
            billdata.setPk_org(list.get(i).getPkOrg());
            billdata.setPk_psndoc(list.get(i).getPkPsndoc());
            billdata.setPk_tradetype(list.get(i).getPkTradetype());
            billdata.setCreationtime(list.get(i).getCreationtime());

            detailList = ncPaybillDetailMapper.selectList(new QueryWrapper<NcPaybillDetail>().eq("pid",list.get(i).getId()));
            listItems.clear();
            for (int j = 0; j < detailList.size(); j++) {
                Items items = new Items();
                items.setCashitem("");
                items.setPayaccount(detailList.get(j).getPayaccount());
                items.setRecaccount(detailList.get(j).getRecaccount());
                items.setDef20(detailList.get(j).getDef20());
                items.setDefitem12("");
                items.setDefitem23("");
                items.setDefitem26("");
                items.setDefitem27("");
                items.setDefitem28("");
                items.setDefitem30("");
                items.setLocal_money_de(detailList.get(j).getLocalMoneyDe());
                items.setLocal_tax_de(detailList.get(j).getLocalTaxDe());
                items.setMoney_de(detailList.get(j).getMoneyDe());
                items.setNotax_de("0.00");
                items.setObjtype(detailList.get(j).getObjtype());
                items.setPk_deptid(detailList.get(j).getPkDeptid());
                items.setPk_psndoc(detailList.get(j).getPkPsndoc());
                items.setRowno(String.valueOf(detailList.get(j).getId()));
                items.setScomment(detailList.get(j).getScomment());
                items.setSupplier(detailList.get(j).getSupplier());
                items.setSzxmid("");
                items.setTaxrate(detailList.get(j).getTaxrate());


                listItems.add(items);

            }
            billdata.setItems(listItems);
            dto.setData(billdata);
            try {
                sendToReviewLog.callEsb(JSON.toJSONString(dto), String.valueOf(list.get(i).getId()));
            }
            catch(Exception ex)
            {
                continue;
            }

        }

        list = ncPaybillMapper.selectList(new QueryWrapper<NcPaybill>().eq("status","0").eq("billclass","yf"));
        billdata = new PaybillData();
        for (int i = 0; i < list.size(); i++) {
            dto = new NCDto();
            detailList = new ArrayList<>();
            listItems = new ArrayList<>();
            BeanUtils.copyProperties(list.get(i),billdata);
            billdata.setPk_balatype(list.get(i).getPkBalatype());
            billdata.setPk_billtype("");
            billdata.setPk_busitype(list.get(i).getPkBusitype());
            billdata.setPk_currtype(list.get(i).getPkCurrtype());
            billdata.setPk_deptid(list.get(i).getPkDeptid());
            billdata.setPk_group(list.get(i).getPkGroup());
            billdata.setPk_org(list.get(i).getPkOrg());
            billdata.setPk_psndoc(list.get(i).getPkPsndoc());
            billdata.setPk_tradetype("");
            billdata.setPk_tradetypeid(list.get(i).getPkTradetype());
            billdata.setLocal_money(list.get(i).getLocalMoney());
            billdata.setCreationtime("");

            detailList = ncPaybillDetailMapper.selectList(new QueryWrapper<NcPaybillDetail>().eq("pid",list.get(i).getId()));
            listItems.clear();
            for (int j = 0; j < detailList.size(); j++) {
                Items items = new Items();
                items.setBillclass("yf");
                items.setPayaccount(detailList.get(j).getPayaccount());
                items.setRecaccount(detailList.get(j).getRecaccount());
                items.setCreationtime(detailList.get(i).getCreationtime());
                items.setCreator(detailList.get(i).getCreator());
                items.setDef20(detailList.get(j).getDef20());
                items.setDef24(detailList.get(j).getDef24());
                items.setDef25(detailList.get(j).getDef25());
                items.setDef28(detailList.get(j).getDef28());
                items.setDef30(detailList.get(j).getDef30());
                items.setFkyt(billdata.getFkyt());
                items.setCaltaxmny(detailList.get(j).getCaltaxmny());
                items.setLocal_money_cr(detailList.get(j).getLocalMoneyDe());
                items.setLocal_tax_cr(detailList.get(j).getLocalTaxCr());
                items.setMoney_bal(detailList.get(j).getLocalMoneyDe());
                items.setObjtype(detailList.get(j).getObjtype());
                items.setPk_deptid(detailList.get(j).getPkDeptid());
                items.setPk_psndoc(detailList.get(j).getPkPsndoc());
                items.setRowno(String.valueOf(detailList.get(j).getRowno()));
                items.setScomment(detailList.get(j).getScomment());
                items.setSupplier(detailList.get(j).getSupplier());
                items.setSzxmid("");
                items.setTaxrate(detailList.get(j).getTaxrate());


                listItems.add(items);

            }
            billdata.setItems(listItems);
            dto.setData(billdata);
            try {
                sendToReviewLog.callEsb(JSON.toJSONString(dto), String.valueOf(list.get(i).getId()));
            }
            catch(Exception ex)
            {
                continue;
            }

        }
        //return sendToReviewLog.callEsb(body,"E9BO01");
        return JsonResult.success();
    }

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/sendncfk/{id}")
    public JsonResult sendncfk(@PathVariable("id") String id) {
        return sendToReviewLog.sendFKOrderById(id);
    }

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/sendncyf/{id}")
    public JsonResult sendncyf(@PathVariable("id") String id) {
        return sendToReviewLog.sendYFOrderById(id);
    }

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/sendorder2")
    public JsonResult sendorder2(@RequestBody String body) {
        //String body = JSON.toJSONString(entity);
        return sendToReviewLog.callEsb(body,"E9BO01");
    }

    @PostMapping("/sendreview")
    public JsonResult sendreview(@RequestBody String body) {
        //String body = JSON.toJSONString(entity);
        return sendToReviewLog.callEsbKdl("","",body);
    }

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/sendorderkdl")
    public JsonResult sendorderkdl(@RequestBody String body) {
        //String body = JSON.toJSONString(entity);
//        String[] result=  NCInterface.callESB("PurchaseIn",body);
//        if("Y".equals(result[4])) {
//            return  JsonResult.success();
//        }else{
//            return JsonResult.error(result[5]+":"+result[6]);
//        }
        return sendToReviewLog.callEsbKdl("PurchaseIn","",body);
    }

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/cgrk/{Djhs}")
    public JsonResult cgrk(@PathVariable("Djhs") String[] Djhs) {
        //String body = JSON.toJSONString(entity);
        return sendToReviewLog.callCGRK(Djhs,"E9BO01");
    }

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/gjtc/{Djhs}")
    public JsonResult gjtc(@PathVariable("Djhs") String[] body) {
        //String body = JSON.toJSONString(entity);
        return sendToReviewLog.callGJTC(body,"E9BO01");
    }

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/xsck/{Djhs}")
    public JsonResult xsck(@PathVariable("Djhs") String[] body) {
        //String body = JSON.toJSONString(entity);
        return sendToReviewLog.callXSCK(body,"E9BO01");
    }

    @Log(title = "", logType = LogType.INSERT)
    @PostMapping("/xsth/{Djhs}")
    public JsonResult xsth(@PathVariable("Djhs") String[] body) {
        //String body = JSON.toJSONString(entity);
        return sendToReviewLog.callXSTH(body,"E9BO01");
    }

}
